package com.itcast.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.itcast.domain.Product;
import com.itcast.utils.JDBCUtils;

public class ProductDao {
	QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());

	public List<Product> findAll(int pageNo, int pageSize) throws SQLException {
		String sql = "select * from product limit ?,?";
		List<Product> list = queryRunner.query(sql, new BeanListHandler<Product>(Product.class),
				(pageNo - 1) * pageSize, pageSize);
		return list;
	}

	public int finTotalCount() throws SQLException {
		String sql = "select count(*) from product";
		Long totalCount = (Long) queryRunner.query(sql, new ScalarHandler());
		return totalCount.intValue();
	}

}
